package org.soretro.portfolio.view.contacts
{
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.managers.SystemManager;
	
	import org.robotlegs.mvcs.Mediator;
	import org.soretro.portfolio.helpers.GoogleAnalytics;
	
	public class ContactsPopupMediator extends Mediator
	{
		[Inject]
		public var view:ContactsPopupRenderer;
		
		public var tracker:AnalyticsTracker;
		
		override public function onRegister():void
		{
			super.onRegister();
//			googleTracker();
			
//			GoogleAnalytics.getInstance().send( "Contact/open" );
//			tracker.trackPageview( "Contact/open" );
			
			for ( var i:int=0; i < view.contactList.numChildren;i++ ) {
				var contactItem:ContactsListRenderer = ContactsListRenderer( view.contactList.getChildAt(i) );
				contactItem.addEventListener( MouseEvent.CLICK, onIconClick );
			}
		}

		protected function onIconClick( event:MouseEvent ):void
		{
			gotoUrl( event.currentTarget.url, event.currentTarget.id );
		}
		
		protected function gotoUrl( url:String, id:String ):void
		{
			GoogleAnalytics.getInstance().send( "Contact/"+ id );
//			tracker.trackPageview( "Contact/"+ id );
			var request:URLRequest = new URLRequest( url );
			navigateToURL( request );
		}
		
		private function googleTracker():void
		{
			tracker = new GATracker( SystemManager.getSWFRoot(this).stage, "UA-18699334-1", "AS3", false );
		}
	}
}